﻿using SkypeBot.Core;
using SkypeBot.Commands;
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace Commands
{
    public class NewsCommand : IMessageHandler
    {
        private Article mobjNewsList { get; set; }

        private bool blnIsLaunched;

        private Thread objWorker;

        //public Responce GetNews(Message message)
        //{
        //    if (mobjNewsList == null)
        //    {
        //        mobjNewsList = new Article();
        //    }
        //    mobjNewsList.Articles = RSSParser.GetRecords("http://tsn.ua/rss", Encoding.UTF8);
        //    Regex objRegex = new Regex("\\\".*/>");
        //    Regex objTab = new Regex(" {2,}");
        //    string strDescription = mobjNewsList.Articles[0].Description.Replace(" <img src=\"", "Картинка: ");
        //    strDescription = objRegex.Replace(strDescription, "");
        //    strDescription = objTab.Replace(strDescription, "");
        //    string strResponce = string.Format("{0}\r\n{1}\r\nДокладніше: {2}\r\n", mobjNewsList.Articles[0].Title, strDescription,mobjNewsList.Articles[0].Link);
        //    return new Responce(strResponce);
        //}

        [MessageAttribute(@"^!news$|новости$|новини$")]
        public Responce SwitchNewsMonitoring(Message message)
        {
            blnIsLaunched = !blnIsLaunched;
            if (blnIsLaunched)
            {
                if (objWorker == null)
                {
                    objWorker = new Thread(NewsMonitor);
                }
                objWorker.Start();
            }
            else { objWorker.Abort(); objWorker = null; }
            string strStatus = blnIsLaunched ? "On" : "Off";
            return new Responce(string.Format("News monitoring is {0}",strStatus));
        }

        public void NewsMonitor()
        {
            while (true)
            {
                try
                {
                    if (mobjNewsList == null)
                    {
                        mobjNewsList = new Article();
                        mobjNewsList.Articles = RSSParser.GetRecords("http://tsn.ua/rss", Encoding.UTF8);
                        SendProccessedMessage(mobjNewsList);
                    }
                    else
                    {
                        var objArticles = RSSParser.GetRecords("http://tsn.ua/rss", Encoding.UTF8);
                        if (mobjNewsList.Articles[0].Title != objArticles[0].Title)
                        {
                            mobjNewsList.Articles = objArticles;

                            SendProccessedMessage(mobjNewsList);
                        }
                    }
                }
                catch(Exception ex)
                {
                    foreach (var objReciever in Core.Instance.Receivers)
                    {
                         Core.Instance.SendMessage(new Responce(objReciever, ex.Message));
                    }
                }

                Thread.Sleep(new TimeSpan(0,0,30));
            }
        }

        void SendProccessedMessage(Article objArticle)
        {
            Regex objRegex = new Regex("\\\".*/>");
            Regex objTab = new Regex(" {2,}");
            string strDescription = objArticle.Articles[0].Description.Replace(" <img src=\"", "Картинка: ");
            strDescription = objRegex.Replace(strDescription, "");
            strDescription = objTab.Replace(strDescription, "");
            string strResponce = string.Format("{0}\r\n{1}\r\nДокладніше: {2}\r\n", objArticle.Articles[0].Title, strDescription, objArticle.Articles[0].Link);
            foreach (var objReciever in Core.Instance.Receivers)
            {
                Core.Instance.SendMessage(new Responce(objReciever, strResponce));
            }
        }

    }
}
